|
|
|
|
|
|
|
3. Declare form-level variables, cn and rs, to represent ADO connection and recordset objects. |
|
|
|
|
|
|
|
|
4. Draw a command button, cmdFill, on the form and label it Fill Recordset. |
|
|
|
|
|
|
|
|
5. Add code to the form's Load and Unload events to open and close a connection to the BIBLIO data source. |
|
|
|
|
|
|
|
|
6. Add code to the button's Click event to populate the recordset with the following SQL query: |
|
|
|
|
|
|
|
|
"Select * from Titles where [Year Published] >= 1996" |
|
|
|
|
|
|
|
|
The complete code for the form up to this point is shown in Listing 29.1. |
|
|
|
|
|
|
|
|
Listing 29.1 RPTDEMO.ZIPGetting Some Sample Data for the Report |
|
|
|
|
|
|
|
|
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Private Sub cmdFill_Click()
Set rs = cn.Execute("Select * from Titles where [Year Published] >= 1996")
MsgBox "Recordset populated."
End Sub
Private Sub Form_Load()
Set cn = New ADODB.Connection
cn.Open "DSN=BIBLIO"
End Sub
Private Sub Form_Unload(Cancel As Integer)
rs.Close
cn.Close
End Sub |
|
|
|
|
|
|
|
|
The preceding query creates a recordset containing some records from the Titles table of the BIBLIO sample database. The records were limited to those titles published in or after 1996 to make things quicker because the table has a lot of records in it. When you click the command button, the recordset object rs is populated with the fields from the Titles table. Next, you add a DataReport object to the project and learn how to create a report from the sample recordset. |
|
|
|
|
|
|
|
|
Adding a Data Report to Your Project |
|
|
|
|
|
|
|
|
Now that you have data, the next step is to determine how to display that data on a report. First, you need to add a new DataReport object to your Visual Basic project. To do so, choose Project, Add Data Report. You should notice that several things happen immediately: |
|
|
|
|
|
|
|
|
A new DataReport object called DataReport1 is added to the Project Explorer window. |
|
|
|
|
|
|
|
|
A new DataReport area is added to the Toolbox. |
|
|
|
|
|
|
|
|
The Report Designer window appears, as shown in Figure 29.1 |
|
|
|
|
|